﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace CrazyClient
{
    [XmlRoot("AssetInfoRoot")]
    public class AssetInfo : IXmlSerializable
    {
        public AssetInfo()
        {
        }
        public AssetInfo(Guid id, string name, Guid version)
        {
            _id = id;
            _name = name;
            _version = version;
        }
        Guid _id, _version;
        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
        }
        public Guid Id
        {
            get
            {
                return _id;
            }
        }
        public Guid Version
        {
            get
            {
                return _version;
            }
        }
        public static AssetInfo Load(string xmlFile)
        {
            FileStream stream = new FileStream(xmlFile, FileMode.OpenOrCreate);
            XmlSerializer x = new XmlSerializer(typeof(AssetInfo));

            AssetInfo si = (AssetInfo)x.Deserialize(stream);
            stream.Close();
            return si;
        }
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.ReadStartElement("AssetInfoRoot");
            this._id = new Guid((string)reader.ReadElementContentAsObject("_id", ""));
            this._version = new Guid((string)reader.ReadElementContentAsObject("_version", ""));
            this._name = (string)reader.ReadElementContentAsObject("_name", "");
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("_id");
            writer.WriteValue(this._id.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_version");
            writer.WriteValue(this._version.ToString());
            writer.WriteEndElement();

            writer.WriteStartElement("_name");
            writer.WriteValue(this._name.ToString());
            writer.WriteEndElement();

        }

        #endregion
    }
}
